package serviceimpl;

import bean.Dispatcher;
import bean.UserLogin;
import bean.Usermes;
import dao.UserLoginMapper;
import org.apache.ibatis.session.SqlSession;
import service.RegService;
import util.CreateSqlSession;

import java.io.IOException;

public class RegServiceImpl implements RegService {
    @Override
    public boolean isReg(UserLogin userLogin) throws IOException {
        SqlSession sqlSession = CreateSqlSession.createSqlSession();
        UserLoginMapper userLoginMapper=sqlSession.getMapper(UserLoginMapper.class);
        if(userLoginMapper.getUserByName(userLogin.getUsername())==null){
            userLoginMapper.insertUser(userLogin);
            //如果注册的用户是普通用户
            if(userLogin.getUsertype()==1){
                userLoginMapper.insertgeneralUser(new Usermes(userLogin.getUserid(),userLogin.getUsername()));
                return true;

            }
            //否则插入配送员信息
            else {
                userLoginMapper.insertDispather(new Dispatcher(userLogin.getUserid(),userLogin.getUsername()));
            return true;}
        }
        else
        return false;
    }
}
